package com.example.carrierapp.ui.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

public class GuidePageAdapter extends PagerAdapter {

    private List<View> viewList = new ArrayList<>();

    public GuidePageAdapter(int[] ids, Context context) {
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        for (int i = 0; i < ids.length; i++) {
            //new ImageView并设置全屏和图片资源
            ImageView imageView = new ImageView(context);
            imageView.setLayoutParams(params);
            imageView.setBackgroundResource(ids[i]);
            //将ImageView加入到集合中
            viewList.add(imageView);
        }
    }

    /**
     * @return 返回页面的个数
     */
    @Override
    public int getCount() {
        return viewList != null ? viewList.size() : 0;
    }

    /**
     * 判断对象是否生成界面
     *
     * @param view
     * @param object
     * @return
     */
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    /**
     * 初始化position位置的界面
     *
     * @param container
     * @param position
     * @return
     */
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        container.addView(viewList.get(position));
        return viewList.get(position);
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(viewList.get(position));
    }
}
